package com.example.chongwushangcheng.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.petstore.PetStoreDetailActivity;
import com.example.petshopping.R;
import com.example.petshopping.adapter.StoreAdapter;
import com.example.petshopping.model.Product;
import com.example.petshopping.model.Store;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class LocalPetStoreActivity extends AppCompatActivity implements StoreAdapter.OnStoreClickListener {

    private RecyclerView rvStores;
    private List<Store> storeList;
    private StoreAdapter storeAdapter;
    private LinearLayout header;
    private ScrollView scrollView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_local_pet_store);

        // 初始化返回按钮点击事件
        ImageView ivBack = findViewById(R.id.iv_back);
        ivBack.setOnClickListener(v -> finish());

        // 获取顶部布局引用
        header = findViewById(R.id.header);
        scrollView = findViewById(android.R.id.content).getRootView().findViewById(R.id.header).getParent().getParent() instanceof ScrollView ? 
                     (ScrollView) findViewById(android.R.id.content).getRootView().findViewById(R.id.header).getParent().getParent() : null;

        // 初始化店铺列表
        rvStores = findViewById(R.id.rv_stores);
        rvStores.setLayoutManager(new LinearLayoutManager(this));
        rvStores.setFocusable(false); // 确保RecyclerView不可获取焦点
        rvStores.setNestedScrollingEnabled(false); // 禁用嵌套滚动

        // 准备数据
        initData();

        // 使用项目中已有的StoreAdapter
        storeAdapter = new StoreAdapter(this, storeList, this);
        rvStores.setAdapter(storeAdapter);

        // 确保页面打开时聚焦在顶部
        ensureFocusOnTop();
    }

    @Override
    protected void onResume() {
        super.onResume();
        // 在页面恢复可见时，再次确保焦点在顶部
        new Handler().postDelayed(this::ensureFocusOnTop, 100);
    }

    /**
     * 确保页面打开时聚焦在顶部
     */
    private void ensureFocusOnTop() {
        // 请求顶部布局获取焦点
        header.requestFocus();
        
        // 防止RecyclerView自动获取焦点
        rvStores.clearFocus();
        
        // 如果有ScrollView，强制滚动到顶部
        if (scrollView != null) {
            scrollView.smoothScrollTo(0, 0);
        }
        
        // 延迟执行，确保在页面完全加载后执行
        new Handler().postDelayed(() -> {
            // 再次确保顶部布局获取焦点
            header.requestFocus();
            
            // 再次强制滚动到顶部
            if (scrollView != null) {
                scrollView.fullScroll(ScrollView.FOCUS_UP);
                scrollView.smoothScrollTo(0, 0);
            }
        }, 200);
    }

    private void initData() {
        storeList = new ArrayList<>();

        // 准备商品数据 - 这里为4家店铺准备了相同的商品数据
        List<Product> products = Arrays.asList(
                new Product("1", "水族箱", "pet_3", "¥432"),
                new Product("2", "仓鼠", "pet_2", "¥432"),
                new Product("3", "水族植物", "pet_4", "¥432"),
                new Product("4", "宠物用品", "pet_5", "¥432")
        );

        // 添加店铺数据
        for (int i = 0; i < 4; i++) {
            storeList.add(new Store(
                    String.valueOf(i + 1),
                    "招财猫旺财狗",
                    "yingwu",
                    "★★★★★",
                    "44万粉丝",
                    products
            ));
        }
    }

    @Override
    public void onStoreClick(Store store) {
        Toast.makeText(this, "点击店铺: " + store.getName(), Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onEnterStoreClick(Store store) {
        // 跳转到PetStoreDetailActivity
        Intent intent = new Intent(this, PetStoreDetailActivity.class);
        // 可以传递店铺信息
        intent.putExtra("storeName", store.getName());
        intent.putExtra("storeRating", store.getRating());
        intent.putExtra("storeFollowers", store.getFollowers());
        startActivity(intent);
    }
} 